package org.sidora.core;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;
import java.util.StringTokenizer;
import org.sidora.core.util.AbstractFactory;
import org.sidora.core.util.AbstractInventory;
import org.sidora.core.util.OCProvider;
import org.sidora.core.util.ParameterCollector;


/**
 * Classe que representa el valor d'una variable d'interes de tipus String normalitzada
 * La classe guarda una referencia al objecte al que descriu, el nom de la variable, el valor i el
 * tipus de categories de que forma part.
 * @author Enric Tartera, Anna Arnalot, Juan Manuel Gimeno, Roger Masgoret
 * @version 1.0
 */

public class Category implements Serializable, Comparable, Cloneable{
    
    private String value;
    private Describer type;
   // private String label;
    
    private Category(){
        super();
    }
    public String getValue() {
        StringTokenizer st = new StringTokenizer(value,"]");
        if (st.countTokens()>1){
            String finalvalue = "";
            st.nextToken();
            finalvalue = st.nextToken().trim();
            return finalvalue;
        }else return value;
        
    }
    public void setValue(String value) {
        this.value = value;
    }
    public Describer getType() {
        return type;
    }
    public void setType(Describer type) {
        this.type = type;
    }
    public boolean equals(Object obj) {
        if (obj == this)	return true;
        if (!(obj instanceof Category)) return false;
        Category cat = null;
        try {
            cat = (Category) obj;
        } catch (Exception e) {
            return false;
        }
        if (!this.getType().equals(cat.getType())) return false;
        if (!this.value.equals(cat.value)) return false;
        return true;
    }
    
    public @Override Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.value;
    }
    
    
    public static class Inventory extends AbstractInventory{
        
        public boolean exist(final Category cat) throws Exception{
            ObjectContainer db = OCProvider.getInstance();
            ObjectSet<Category> categories = db.query(new Predicate<Category>() {
                
                public boolean match(Category candidate) {
                    return candidate.equals(cat);
                }
            });
            
            if (categories.hasNext()){
                return true;
            } else {
                return false;
            }
        }
        
        public Category getNewObject(ParameterCollector param) throws Exception{
            Category cat = new Category();
            
            cat.setValue((String)param.get("value"));
            cat.setType((Describer)param.get("type"));
           // cat.setLabel((String)param.get("label"));
            
            return cat;
        }
        
        
        
        
        public Object getObject(ParameterCollector param) {
            
            return null;
        }
        
        @Override
        protected KeyObject getNewKey(ParameterCollector param) {
            // TODO Auto-generated method stub
            return null;
        }
        
        
        public static List getList() {
            ObjectContainer db = OCProvider.getInstance();
            List<Category> list = db.query(Category.class);
            
            return list;
        }
        
        
        
        public static List getList(final Describer categoryType) {
            ObjectContainer db = OCProvider.getInstance();
            List<Category> list = db.query(new Predicate<Category>(){;
            
            @Override
            public boolean match(Category category) {
                return category.getType() == categoryType;
            }});
            
            return list;
    }
    
}

public List getList() {
    
    ObjectContainer db = OCProvider.getInstance();
    List<Category> list = db.query(Category.class);
    
    return list;
    
}

public int compareTo(Object obj) {
    Category cat = (Category) obj;
    return this.value.compareTo(cat.value);
}

public String getLabel() {
      StringTokenizer st = new StringTokenizer(value,"]");
        if (st.countTokens()>1){
            String finalvalue = "";
            finalvalue = st.nextToken().trim().replace("[","");
            return finalvalue;
        }else return null;
    
}


}